package com.bankLogisticsSys.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bankLogisticsSys.util.WeixinUtil;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;

public class ActionInterceptor implements Interceptor{

	@Override
	public void intercept(Invocation inv) {
//		Cache actionCache = Redis.use();
		if(inv.getController().getPara("code") == null){
			String paraMsg = "";
			if(inv.getActionKey().equals("/weixin/login")){
				paraMsg = "?employeeID="+inv.getController().getPara("employeeID") +
						"&password=" + inv.getController().getPara("password");
			}else if(inv.getActionKey().equals("/weixin/modifyPassword")){
				paraMsg = "?oldPassword="+inv.getController().getPara("oldPassword") +
						"&newPassword=" + inv.getController().getPara("newPassword");
			}else if(inv.getActionKey().equals("/weixin/get/reject")){
				paraMsg = "?trackingID="+ inv.getController().getPara("trackingID");
			}
			String appId = WeixinUtil.getApiConfig().getAppId();
			String redirectUri = WeixinUtil.urlEncodeUTF8(PropKit.get("domain") + inv.getActionKey()+paraMsg);
			String state = System.currentTimeMillis() + "";
			String url = SnsAccessTokenApi.getAuthorizeURL(appId, redirectUri, state, false);
			inv.getController().setAttr("redirecturl", url);
			inv.getController().render("/WEB-INF/content/action/oauth.html");
		}else{
			inv.invoke();
		}
	}
}
